var setting = {
	view: {
		addHoverDom: addHoverDom,
		removeHoverDom: removeHoverDom,
		selectedMulti: true,
		showIcon:false,
		showLine:true
	},
	check: {
		enable: true
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	edit: {
		enable: true
	},
	callback: {
		beforeRemove: beforeRemove,
		beforeRename: beforeRename,
		onCollapse: zTreeOnCollapse,
		onExpand: zTreeOnExpand,
		onClick: zTreeOnclick
	}
};

//初始化节点
$.ajax({
	'url':'data.php',
	'data':{'action':'init_data_list'},
	'dataType':'json',
	'type':'post',
	'success':function(data){
		if(data==0){
			alert("数据库连接失败");
		}else{
			if(data==1){
				alert("树加载失败");
			}else{
				var zNodes = data;				
				$(document).ready(function(){
					$.fn.zTree.init($("#treeDemo"), setting, zNodes);
				});	
			}
		}
				
	},
		
});
//点击节点展示
function zTreeOnclick(event, treeId, treeNode) {
	if(treeNode.isParent!=true){
		$.ajax({
			'url':'data.php',
			'data':{'action':'OnclickLike','id':treeNode.id},
			'dataType':'html',
			'type':'POST',
			 // 请求发送之前（发送请求前可修改XMLHttpRequest对象的函数，如添加自定义HTTP头。）。
			beforeSend:function(XMLHttpRequest){
				$("#boxs").html("<div class='loading'>       <span></span>      <span></span>      <span></span>      <span></span>    <span></span></div>"); 
			}, 
			'success':function(data){
				if(data==2){
					alert('节点已删除');
				}else{
					$("#boxs").html(data);
				}
					
			},				
		});
	}
	
};
//添加节点
var newCount = 1;
function addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
	var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
		+ "' title='add node' onfocus='this.blur();'></span>";
	sObj.after(addStr);
	var btn = $("#addBtn_"+treeNode.tId);
	if (btn) btn.bind("click", function(){
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");				
		$.ajax({
			'url':'data.php',
			'data':{'action':'addNodes','pId':treeNode.id,'name':"new node" + (newCount)},
			'dataType':'json',
			'type':'post',
			'success':function(data){
				if(data.error==1) {
					zTree.addNodes(treeNode, {id:data.id.id, pId:treeNode.id, name:"new node" + (newCount++)});						
				}else{
					if(data.error==2){
						alert("名称不能为空");							
					}else{
						alert("添加失败");
					}
					
				};						
				//$(document).ready(function(){
			
					//$("#boxs").html(data.id.id);
				//});						
			},				
		});
		return false;
		
	});
};
//重命名前
function beforeRename(treeId, treeNode, newName, isCancel) {
	if (newName.length == 0) {
		setTimeout(function() {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.cancelEditName();
			alert("节点名称不能为空.");
		}, 0);
		return false;
	}
	return true;
}

//删除节点前
function beforeRemove(treeId, treeNode) {			
	return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
}
function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.tId).unbind().remove();
};
//节点折叠
function zTreeOnCollapse(event, treeId, treeNode) {
	$.ajax({
		'url':'data.php',
		'data':{'action':'OnCollapse','id':treeNode.id},
		'dataType':'json',
		'type':'post',

		'success':function(data){
			
		},				
	});
};
//节点展开
function zTreeOnExpand(event, treeId, treeNode) {
	$.ajax({
		'url':'data.php',
		'data':{'action':'OnExpand','id':treeNode.id},
		'dataType':'json',
		'type':'post',

		'success':function(data){
			
		},				
	});
};